﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SC2Ranks.cs" company="DarkBone">
//   DarkBone
// </copyright>
// <summary>
//   The sc2ranks.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DarkBone.Engine
{
   using System;
   using System.Diagnostics.CodeAnalysis;
   using System.IO;
   using System.Net;
   using System.Text;

   using Newtonsoft.Json;

   /// <summary>
   /// The s c 2 ranks portrait.
   /// </summary>
   public struct SC2RanksPortrait
   {
      #region Constants and Fields

      /// <summary>
      ///   The column.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int column;

      /// <summary>
      ///   The icon_id.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public int icon_id;

      /// <summary>
      ///   The row.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int row;

      #endregion
   }

   /// <summary>
   /// The s c 2 ranks team.
   /// </summary>
   public struct SC2RanksTeam
   {
      #region Constants and Fields

      /// <summary>
      ///   The bracket.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int bracket;

      /// <summary>
      ///   The division_rank.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public int division_rank;

      /// <summary>
      ///   The fav_race.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public string fav_race;

      /// <summary>
      ///   The is_random.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public bool is_random;

      /// <summary>
      ///   The league.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public string league;

      /// <summary>
      ///   The losses.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int losses;

      /// <summary>
      ///   The points.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int points;

      /// <summary>
      ///   The ratio.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public string ratio;

      /// <summary>
      ///   The region_rank.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public int region_rank;

      /// <summary>
      ///   The updated_at.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public DateTime updated_at;

      /// <summary>
      ///   The wins.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int wins;

      /// <summary>
      ///   The world_rank.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public int world_rank;

      #endregion
   }

   /// <summary>
   /// The sc2 ranks player info.
   /// </summary>
   public struct SC2RanksPlayerInfo
   {
      #region Constants and Fields

      /// <summary>
      ///   The achievement_points.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public int achievement_points;

      /// <summary>
      ///   The bestdivision.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public string bestdivision;

      /// <summary>
      ///   The bnet_id.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public int bnet_id;

      /// <summary>
      ///   The character_code.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public int character_code;

      /// <summary>
      ///   The fav_race.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public string fav_race;

      /// <summary>
      ///   The id.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int id;

      /// <summary>
      ///   The name.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public string name;

      /// <summary>
      ///   The portrait.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public SC2RanksPortrait portrait;

      /// <summary>
      ///   The region.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public string region;

      /// <summary>
      ///   The teams.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public SC2RanksTeam[] teams;

      /// <summary>
      ///   The totalwins.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      public int totalwins;

      /// <summary>
      ///   The updated_at.
      /// </summary>
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1307:AccessibleFieldsMustBeginWithUpperCaseLetter",
         Justification = "Reviewed. Suppression is OK here.")]
      [SuppressMessage("StyleCop.CSharp.NamingRules", "SA1310:FieldNamesMustNotContainUnderscore",
         Justification = "Reviewed. Suppression is OK here.")]
      public DateTime updated_at;

      #endregion
   }

   /// <summary>
   /// The s c 2 ranks.
   /// </summary>
   public class SC2Ranks
   {
      #region Constructors and Destructors

      #endregion

      #region Public Methods

      /// <summary>
      /// The get deserialized object.
      /// </summary>
      /// <param name="json">
      /// The json.
      /// </param>
      /// <returns>
      /// returns SC2RanksPlayerInfo
      /// </returns>
      public static SC2RanksPlayerInfo GetDeserializedObject(string json)
      {
         return !string.IsNullOrEmpty(json)
                   ? JsonConvert.DeserializeObject<SC2RanksPlayerInfo>(json)
                   : new SC2RanksPlayerInfo();
      }

      /// <summary>
      /// The retrieve player info json.
      /// </summary>
      /// <param name="realm">
      /// The realm.
      /// </param>
      /// <param name="character">
      /// The character.
      /// </param>
      /// <param name="account">
      /// The account.
      /// </param>
      /// <returns>
      /// The retrieve player info json.
      /// </returns>
      [SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1625:ElementDocumentationMustNotBeCopiedAndPasted",
         Justification = "Reviewed. Suppression is OK here.")]
      public static string RetrievePlayerInfoJSON(string realm, string character, string account)
      {
         var bnetid = account.Substring(account.LastIndexOf('-') + 1);
         var url = string.Format("http://sc2ranks.com/api/base/teams/{0}/{1}!{2}.json?appKey=darkbone.com", realm, character, bnetid);
         var request = (HttpWebRequest)WebRequest.Create(url);
         request.Method = "GET";
         request.Credentials = CredentialCache.DefaultCredentials;
         request.ContentType = "text/xml"; // depending on the data u are sending this
         request.Timeout = 2000;
         HttpWebResponse response;
         try
         {
            response = (HttpWebResponse)request.GetResponse();
            var responseStream = response.GetResponseStream();
            if (responseStream == null)
            {
               return string.Empty;
            }

            var streamReader = new StreamReader(responseStream, Encoding.UTF8);
            var responseText = streamReader.ReadToEnd();
            response.Close();
            return responseText;
         }
         catch (WebException)
         {
            // handle exception
         }

         return string.Empty;
      }

      #endregion
   }
}